﻿namespace Smart.Utils.UI.BarcodeLib
{
    using System;

    internal class UPCE : BarcodeCommon, IBarcode
    {
        private string[] EAN_CodeA = new string[] { "0001101", "0011001", "0010011", "0111101", "0100011", "0110001", "0101111", "0111011", "0110111", "0001011" };
        private string[] EAN_CodeB = new string[] { "0100111", "0110011", "0011011", "0100001", "0011101", "0111001", "0000101", "0010001", "0001001", "0010111" };
        private string[] EAN_CodeC = new string[] { "1110010", "1100110", "1101100", "1000010", "1011100", "1001110", "1010000", "1000100", "1001000", "1110100" };
        private string[] EAN_Pattern = new string[] { "aaaaaa", "aababb", "aabbab", "aabbba", "abaabb", "abbaab", "abbbaa", "ababab", "ababba", "abbaba" };
        private string[] UPCE_Code_0 = new string[] { "bbbaaa", "bbabaa", "bbaaba", "bbaaab", "babbaa", "baabba", "baaabb", "bababa", "babaab", "baabab" };
        private string[] UPCE_Code_1 = new string[] { "aaabbb", "aababb", "aabbab", "aabbba", "abaabb", "abbaab", "abbbaa", "ababab", "ababba", "abbaba" };

        public UPCE(string input)
        {
            base.Raw_Data = input;
        }

        private string Encode_UPCE()
        {
            if (((base.Raw_Data.Length != 6) && (base.Raw_Data.Length != 8)) && (base.Raw_Data.Length != 12))
            {
                throw new Exception("EUPCE-1: Invalid data length. (8 or 12 numbers only)");
            }
            if (!BarcodeEdit.CheckNumericOnly(base.Raw_Data))
            {
                throw new Exception("EUPCE-2: Numeric only.");
            }
            int index = int.Parse(base.Raw_Data[base.Raw_Data.Length - 1].ToString());
            char ch2 = base.Raw_Data[0];
            int num2 = int.Parse(ch2.ToString());
            if (base.Raw_Data.Length == 12)
            {
                string str = "";
                string str2 = base.Raw_Data.Substring(1, 5);
                string s = base.Raw_Data.Substring(6, 5);
                if ((num2 != 0) && (num2 != 1))
                {
                    throw new Exception("EUPCE-3: Invalid Number System (only 0 & 1 are valid)");
                }
                if ((str2.EndsWith("000") || str2.EndsWith("100")) || (str2.EndsWith("200") && (int.Parse(s) <= 0x3e7)))
                {
                    ch2 = str2[2];
                    str = (str + str2.Substring(0, 2)) + s.Substring(2, 3) + ch2.ToString();
                }
                else if (str2.EndsWith("00") && (int.Parse(s) <= 0x63))
                {
                    str = (str + str2.Substring(0, 3)) + s.Substring(3, 2) + "3";
                }
                else if (str2.EndsWith("0") && (int.Parse(s) <= 9))
                {
                    str = (str + str2.Substring(0, 4)) + (s[4].ToString()) + "4";
                }
                else
                {
                    if ((str2.EndsWith("0") || (int.Parse(s) > 9)) || (int.Parse(s) < 5))
                    {
                        throw new Exception("EUPCE-4: Illegal UPC-A entered for conversion.  Unable to convert.");
                    }
                    str = str + str2 + s[4];
                }
                base.Raw_Data = str;
            }
            string str4 = "";
            if (num2 == 0)
            {
                str4 = this.UPCE_Code_0[index];
            }
            else
            {
                str4 = this.UPCE_Code_1[index];
            }
            string str5 = "101";
            int num3 = 0;
            foreach (char ch in str4)
            {
                int num4 = int.Parse(base.Raw_Data[num3++].ToString());
                switch (ch)
                {
                    case 'a':
                        str5 = str5 + this.EAN_CodeA[num4];
                        break;

                    case 'b':
                        str5 = str5 + this.EAN_CodeB[num4];
                        break;
                }
            }
            return (str5 + "01010" + "1");
        }

        public string Encoded_Value
        {
            get
            {
                return this.Encode_UPCE();
            }
        }
    }
}

